package com.sekill.order.controller;

import cn.hutool.core.lang.Console;
import cn.hutool.http.Header;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.seckill.common.core.web.domain.AjaxResult;
import com.seckill.system.api.RemoteGoodsService;
import com.seckill.system.api.RemoteTbUserService;
import com.sekill.order.config.MapUtil;
import com.sekill.order.config.SignatureUtil;
import com.sekill.order.entity.Mch;
import com.sekill.order.entity.Notifyentity;
import com.sekill.order.entity.Order;
import com.sekill.order.entity.Unifiedorder;
import com.sekill.order.mapper.OrderMapper;
import com.sekill.order.service.IOrderService;
import com.sekill.order.service.PayService;
import org.checkerframework.checker.units.qual.A;
import org.json.JSONException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.nio.charset.Charset;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;

@CrossOrigin
@RestController
@RequestMapping("pay")
public class PayController {


    @Autowired
    RemoteGoodsService remoteGoodsService;

    @Value("${pay.mchId}")
    private String mchId;

    @Value("${pay.mchKey}")
    private String mchKey;

    @Autowired
    OrderMapper orderMapper;

    @Autowired
    private PayService payService;

    @Autowired
    private RemoteTbUserService remoteTbUserService;

    //下单
    @PostMapping("addOrder")
    public AjaxResult addOrder(Order order) throws JSONException {
        //获取userid
        Long userId = remoteTbUserService.getUserId(order.getUid());

        order.setUserId(userId);

        //调用支付下单接口 payService.underOrder(orderId,totalPrice,note),返回支付系统的订单Id，修改自己的订单支付订单Id
        String id = payService.underOrder(order.getOrderId() + "", order.getTotalPay() + "", "whl的订单");

        order.setPromotionIds(id);
        orderMapper.updateById(order);
        return AjaxResult.success(order.getOrderId());
    }

    //支付
    @PostMapping("pay")
    public AjaxResult pay(Order order) throws JSONException {
        //调用支付接口payService.getPayInfo()，拿到信息就可以掉用支付接口

        String s = payService.payInfo(order.getOrderId() + "");
        //调用支付接口payService.pay()，拿到信息就可以掉用支付接口
        boolean b=payService.pay(s);
        //支付成功，改变订单状态
        if (b){
            order.setPaymentType(true);
            order.setPayStatus(1);
            orderMapper.updateById(order);
        }
        return AjaxResult.success(b);
    }

    @PostMapping("notify")
    public  String myNOtify(Notifyentity notifyentity){
        //支付成功
        if (notifyentity.getStatus().equals("success")){
            //订单中支付的状态为已支付，则告诉第三方接口说支付成功
            Order order = orderMapper.selectById(notifyentity.getOutTradeNo());
            if (order.getPayStatus()==1){
                return "sucess";
            }
        }
        return "";
    }


}
